package dialogs.game;

import java.util.ArrayList;

import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;

import persistence.Game;

@SuppressWarnings("serial")
public class GameTableModel extends AbstractTableModel {

	private ArrayList<Game> games;

	/**
	 * Spaltennamen
	 */
	private String[] columnNames = { "Datum", "Team1", "Tore T1", "Tore T2",
			"Team2" };

	public GameTableModel() {
		setGames(new ArrayList<Game>());
	}

	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return getGames().size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Object date = null;
		Game game = getGames().get(rowIndex);
		switch (columnIndex) {
		case 0:
			date = game.getDate();
			break;
		case 1:
			date = new String();
			if (game.getPlayer1Team1() != null) {
				date = game.getPlayer1Team1().toString();
				if (game.getPlayer2Team1() != null)
					date += "; ";
			}
			if (game.getPlayer2Team1() != null)
				date += game.getPlayer2Team1().toString();
			break;
		case 2:
			date = new Integer(game.getGoalsT1());
			break;
		case 3:
			date = new Integer(game.getGoalsT2());
			break;
		case 4:
			date = new String();
			if (game.getPlayer1Team2() != null) {
				date = game.getPlayer1Team2().toString();
				if (game.getPlayer2Team2() != null)
					date += "; ";
			}
			if (game.getPlayer2Team2() != null)
				date += game.getPlayer2Team2().toString();
			break;
		}
		return date;
	}

	/**
	 * Abfrage, ob Zelle editierbar ist
	 * 
	 * @see javax.swing.table.TableModel#isCellEditable(int, int)
	 */
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	/**
	 * Funktion zum Editieren der Tabelle
	 * 
	 * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
	 */
	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		System.err.println("Invalid call");
	}

	/**
	 * Abfrage der Spaltennamen
	 * 
	 * @see javax.swing.table.TableModel#getColumnName(int)
	 */
	@Override
	public String getColumnName(int columnIndex) {
		return columnNames[columnIndex];
	}

	/**
	 * Abfrage welche Klasse sich in der Spalte befindet
	 * 
	 * @see javax.swing.table.TableModel#getColumnClass(int)
	 */
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return getValueAt(0, columnIndex).getClass();
	}

	/**
	 * Hinzufuegen eines Spiels zur Liste
	 * 
	 * @param game1
	 *            Spiel, das hinzugefuegt wird
	 */
	public void addGame(Game game) {
		getGames().add(game);
		// JTable benachrichtigen
		this.fireTableChanged(new TableModelEvent(this));
	}

	public boolean contains(Game o) {
		return getGames().contains(o);
	}

	/**
	 * @return the games
	 */
	public ArrayList<Game> getGames() {
		return games;
	}

	/**
	 * @param games
	 *            the games to set
	 */
	public void setGames(ArrayList<Game> games) {
		this.games = games;
	}
	
	/**
	 * Abfrage von Spiel in der Tabelle
	 * @param rowIndex
	 * @return
	 */
	public Game getGameAt(int rowIndex) {
		return games.get(rowIndex);
	}
}
